import {getTaskList} from "../../api/activity";

import Util from '../../utils/util.js';

const app = getApp();
Page({
  data: {
    parameter: {
      title: "欢迎来到此面向上"
    },

    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,

    current_index: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;

    Util.chekWxLogin().then(res => {

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getLists() {
    const that = this
    if (that.data.loadend || that.data.loading) {
      return;
    }
    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    filter['status'] = this.data.tabs[this.data.current_index].value;
    filter['actId'] = that.data.activity.id;

    that.setData({loading: true, loadTitle: '正在加载...'});
    let lists = that.data.lists || []
    getTaskList(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;
      lists = app.SplitArray(list, lists);
      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });
      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      app.Tips({title: errmsg})
    })
  },
  changeNav(e) {
    this.setData({
      current_index: e.currentTarget.dataset.v,
      loadend: false,
      loading: false,
      page: 1,
      lists: []
    });

    this.getLists();
  },

  onChooseImage(e) {
    const that = this,
      userInfo = this.data.userInfo;

    let images = this.data.images || []

    wx.chooseImage({
      count: 6,
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        // const url = `${app.globalData.url}/api/add_advice_reply_pic`; // 仅为示例，非真实的接口地址
        tempFilePaths.forEach((e1, index) => {
          images.push(e1)
        })
        that.setData({images: images})
      },
      fail: function (error) {
      }
    });
  },

  previewImage: function (e) {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      loading: false,
      loadend: false,
      loadTitle: '加载更多内容',
      lists: [],
      page: 1,
    })
    this.getLists();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getLists();
  },

  onLoadTabbar: function (e) {//底部tabbar返回数据

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})